package online.heycm.rbac.controller.basic;

import online.heycm.platform.common.entity.result.Optional;
import online.heycm.platform.common.entity.result.PageResult;
import online.heycm.platform.common.entity.result.Result;
import online.heycm.rbac.param.basic.InterfaceAddReq;
import online.heycm.rbac.param.basic.InterfaceEditReq;
import online.heycm.rbac.param.basic.InterfaceListReq;
import online.heycm.rbac.param.basic.InterfacePageReq;
import online.heycm.rbac.param.basic.InterfaceRes;
import online.heycm.rbac.param.basic.ResourceAddReq;
import online.heycm.rbac.param.basic.ResourceEditReq;
import online.heycm.rbac.param.basic.ResourceListReq;
import online.heycm.rbac.param.basic.ResourcePageReq;
import online.heycm.rbac.param.basic.ResourceRes;
import online.heycm.rbac.param.basic.RoleAddReq;
import online.heycm.rbac.param.basic.RoleEditReq;
import online.heycm.rbac.param.basic.RoleInterfaceAddReq;
import online.heycm.rbac.param.basic.RoleRes;
import online.heycm.rbac.param.basic.RoleResourceAddReq;
import online.heycm.rbac.service.basic.RbacService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 基础服务 - 资源权限
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/17 15:08
 */
@RestController
@RequestMapping("/basic/rbac")
public class RbacController {

    @Autowired
    private RbacService rbacService;

    /**
     * 添加角色
     *
     * @param req
     * @return
     */
    @PostMapping("/role/add")
    public Result addRole(@RequestBody @Validated RoleAddReq req) {
        Optional optional = rbacService.addRole(req);
        return Result.optional(optional);
    }

    /**
     * 更新角色
     *
     * @param req
     * @return
     */
    @PostMapping("/role/edit")
    public Result editRole(@RequestBody @Validated RoleEditReq req) {
        Optional optional = rbacService.editRole(req);
        return Result.optional(optional);
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/role/list")
    public Result<List<RoleRes>> listRole() {
        Optional<List<RoleRes>> optional = rbacService.listRole();
        return Result.optional(optional);
    }

    /**
     * 添加接口
     *
     * @param req
     * @return
     */
    @PostMapping("/interface/add")
    public Result addInterface(@RequestBody @Validated InterfaceAddReq req) {
        Optional optional = rbacService.addInterface(req);
        return Result.optional(optional);
    }

    /**
     * 更新接口
     *
     * @param req
     * @return
     */
    @PostMapping("/interface/edit")
    public Result editInterface(@RequestBody @Validated InterfaceEditReq req) {
        Optional optional = rbacService.editInterface(req);
        return Result.optional(optional);
    }

    /**
     * 获取接口列表
     *
     * @param req
     * @return
     */
    @GetMapping("/interface/list")
    public Result<List<InterfaceRes>> listInterface(InterfaceListReq req) {
        Optional<List<InterfaceRes>> optional = rbacService.listInterface(req);
        return Result.optional(optional);
    }

    /**
     * 获取接口列表（分页）
     *
     * @param req
     * @return
     */
    @GetMapping("/interface/page")
    public Result<PageResult<InterfaceRes>> pageInterface(InterfacePageReq req) {
        Optional<PageResult<InterfaceRes>> optional = rbacService.pageInterface(req);
        return Result.optional(optional);
    }

    /**
     * 角色权限
     *
     * @param roleId 角色ID
     * @return
     */
    @GetMapping("/role/interface/list/{roleId}")
    public Result<List<Integer>> listRoleInterface(@PathVariable Integer roleId) {
        Optional<List<Integer>> optional = rbacService.listRoleInterface(roleId);
        return Result.optional(optional);
    }

    /**
     * 设置角色权限（全量更新）
     *
     * @param req
     * @return
     */
    @PostMapping("/role/interface/set")
    public Result addRoleInterface(@RequestBody @Validated RoleInterfaceAddReq req) {
        Optional optional = rbacService.addRoleInterface(req);
        return Result.optional(optional);
    }

    /**
     * 添加资源
     *
     * @param req
     * @return
     */
    @PostMapping("/resource/add")
    public Result addResource(@RequestBody @Validated ResourceAddReq req) {
        Optional optional = rbacService.addResource(req);
        return Result.optional(optional);
    }

    /**
     * 更新资源
     *
     * @param req
     * @return
     */
    @PostMapping("/resource/edit")
    public Result editResource(@RequestBody @Validated ResourceEditReq req) {
        Optional optional = rbacService.editResource(req);
        return Result.optional(optional);
    }

    /**
     * 获取资源列表
     *
     * @param req
     * @return
     */
    @GetMapping("/resource/list")
    public Result<List<ResourceRes>> listResource(ResourceListReq req) {
        Optional<List<ResourceRes>> optional = rbacService.listResource(req);
        return Result.optional(optional);
    }

    /**
     * 获取资源列表（分页）
     *
     * @param req
     * @return
     */
    @GetMapping("/resource/page")
    public Result<PageResult<ResourceRes>> pageResource(ResourcePageReq req) {
        Optional<PageResult<ResourceRes>> optional = rbacService.pageResource(req);
        return Result.optional(optional);
    }

    /**
     * 获取资源列表（树）
     *
     * @return
     */
    @GetMapping("/resource/tree")
    public Result<List<ResourceRes>> treeResource() {
        Optional<List<ResourceRes>> optional = rbacService.treeResource();
        return Result.optional(optional);
    }

    /**
     * 角色资源
     *
     * @param roleId 角色ID
     * @return
     */
    @GetMapping("/role/resource/list/{roleId}")
    public Result<List<Integer>> listRoleResource(@PathVariable Integer roleId) {
        Optional<List<Integer>> optional = rbacService.listRoleResource(roleId);
        return Result.optional(optional);
    }

    /**
     * 设置角色资源（全量更新）
     *
     * @param req
     * @return
     */
    @PostMapping("/role/resource/set")
    public Result addRoleResource(@RequestBody @Validated RoleResourceAddReq req) {
        Optional optional = rbacService.addRoleResource(req);
        return Result.optional(optional);
    }
}
